/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package android.isisufes.ProfileManager.profile;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class General extends Profile {
	public String firstName, lastName, city, state, zipCode, highSchool,
			college, company;

	public boolean isMale;

	public boolean[] interests = new boolean[4];

	public int relationshipStatus, birthYear, birthDay, birthMonth, country;

	public int[] language;

	public String toXML() {
		StringBuilder output = new StringBuilder();
		output.append("<?xml version=\"1.0\" encoding=\"utf-8\"?><resources>");

		output.append("<item name=\"country\">");
		int lanLen = language != null ? language.length : 0;
		for (int i = 0; i < lanLen; i++) {
			output.append(language[i] + ",");
		}
		output.append("</item>");

		output.append("<item name=\"country\">" + country + "</item>");
		output.append("<item name=\"birthMonth\">" + birthMonth + "</item>");
		output.append("<item name=\"birthDay\">" + birthDay + "</item>");
		output.append("<item name=\"birthYear\">" + birthYear + "</item>");
		output.append("<item name=\"relationshipStatus\">" + relationshipStatus	+ "</item>");
		output.append("<item name=\"interests\">" + interests[0] + "," + interests[1] + "," + interests[2] + "," + interests[3] + "," + "</item>");
		output.append("<item name=\"isMale\">" + isMale + "</item>");
		output.append("<item name=\"company\">" + company + "</item>");
		output.append("<item name=\"college\">" + college + "</item>");
		output.append("<item name=\"highSchool\">" + highSchool + "</item>");
		output.append("<item name=\"zipCode\">" + zipCode + "</item>");
		output.append("<item name=\"state\">" + state + "</item>");
		output.append("<item name=\"city\">" + city + "</item>");
		output.append("<item name=\"lastName\">" + lastName + "</item>");
		output.append("<item name=\"firstName\">" + firstName + "</item>");
		output.append("</resources>");
				
		return output.toString();
	}

	public void fromXML(InputSource xml) {
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			MyHandler handler = new MyHandler();
			xr.setContentHandler(handler);
			xr.setErrorHandler(handler);
			xr.parse(xml);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class MyHandler extends DefaultHandler {
		String lastElement;

		boolean begin;

		public void endElement(String uri, String localName, String qName) {
			begin = false;
		}

		public void startElement(String uri, String localName, String qName, Attributes attributes) {
			begin = true;
			for (int i = 0; i < attributes.getLength(); i++) {
				lastElement = attributes.getValue(i);
			}
		}

		public void characters(char[] ch, int start, int length) {
			String s = new String(ch, start, length);
			if (s.compareTo("null") == 0) return;
			if (lastElement == null)
				return;
			if (!begin)
				return;
			if (lastElement.compareTo("language") == 0) {
				// TODO
			} else if (lastElement.compareTo("relationshipStatus") == 0) {
				General.this.relationshipStatus = Integer.parseInt(s);
			} else if (lastElement.compareTo("birthYear") == 0) {
				General.this.birthYear = Integer.parseInt(s);
			} else if (lastElement.compareTo("birthDay") == 0) {
				General.this.birthDay = Integer.parseInt(s);
			} else if (lastElement.compareTo("birthMonth") == 0) {
				General.this.birthMonth = Integer.parseInt(s);
			} else if (lastElement.compareTo("birthYear") == 0) {
				General.this.birthYear = Integer.parseInt(s);
			} else if (lastElement.compareTo("country") == 0) {
				General.this.country = Integer.parseInt(s);
			} else if (lastElement.compareTo("isMale") == 0) {
				General.this.isMale =Boolean.parseBoolean(s);
			} else if (lastElement.compareTo("firstName") == 0) {
				General.this.firstName = s;
			} else if (lastElement.compareTo("lastName") == 0) {
				General.this.lastName = s;
			} else if (lastElement.compareTo("city") == 0) {
				General.this.city = s;
			} else if (lastElement.compareTo("state") == 0) {
				General.this.state = s;
			} else if (lastElement.compareTo("zipCode") == 0) {
				General.this.zipCode = s;
			} else if (lastElement.compareTo("highSchool") == 0) {
				General.this.highSchool = s;
			} else if (lastElement.compareTo("college") == 0) {
				General.this.college = s;
			} else if (lastElement.compareTo("company") == 0) {
				General.this.company = s;
			}
		}
	}
}
